package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.Dto.SetmealDto;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author zhoufei
 */
@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;


    /**
     * 重写自定义套餐新增方法
     * @param setmealDto
     */
    @Override
    public void saveWithDish(SetmealDto setmealDto) {
        //1.保存Setmeal表中的信息
        this.save(setmealDto);

        //2.还有菜品信息表需要存储到setmeal_dish表中
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //在存储setmeal_dish表时，需要存储该套餐的id,使得两张表直之间存在练习
        setmealDishes.stream().map((item)->{
            item.setSetmealId(setmealDto.getId());
            return item;
        }).collect(Collectors.toList());

        //保存Setmeal_dish表所需的信息
        setmealDishService.saveBatch(setmealDishes);
    }

    /**
     * 重写自定义的套餐删除方法
     * @param ids
     */
    @Override
    public void removeWithDish(@RequestParam List<Long> ids) {
        //1.查询套餐的销售状态，启售则不能删除
        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.in(Setmeal::getId,ids);
        queryWrapper.eq(Setmeal::getStatus,1);

        int count = this.count(queryWrapper);
        if (count > 0){
            throw new CustomException("有套餐为启售中，无法删除");
        }

        //2.如果不存在启售状态，则开始删除逻辑
            //2.1先删除套餐表中的信息
            this.removeByIds(ids);
            //2.2在删除关联表套餐和菜品表信息
            LambdaQueryWrapper<SetmealDish> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.in(SetmealDish::getSetmealId,ids);
            setmealDishService.remove(queryWrapper1);

    }

    /**
     * 重写套餐修改方法
     * @param setmealDto
     */
    @Override
    public void updateWithDish(SetmealDto setmealDto) {
        //1.修改套餐表中的信息
        this.updateById(setmealDto);

        //2.修改套餐菜品信息,先移除该套餐在菜品表中的菜品信息
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,setmealDto.getId());
        setmealDishService.remove(queryWrapper);
        //3插入套餐的新菜品信息到套餐表
            //3.1获得新的菜品信息
            List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
            //3.2为新增的套餐菜品添加该套餐的id,作为存入到套餐菜品表中该套餐的外键id,让两表关联上
            setmealDishes= setmealDishes.stream().map((setmealDish) -> {
            setmealDish.setSetmealId(setmealDto.getId());
            return setmealDish;
        }).collect(Collectors.toList());
        //4.将新的套餐菜品存入套餐菜品表
        setmealDishService.saveBatch(setmealDishes);
    }
}
